package com.wty.commons.result;

import lombok.Data;

/**
 * @Author:wangtianyu(王天宇)
 * @Date：Created on 2020/4/20 0020 19:40
 * @Description:
 */

/**
 * 统一接口返回
 */
@Data
public class Result<T> {

    private Integer code; // 状态码
    private String message; // 状态信息;
    private Boolean flag; // 请求状态
    private T data; // 返回的结果信息

    private Result(ResultCode resultCode) {
        this.code = resultCode.code();
        this.message = resultCode.message();
    }

    public static <T> Result<T> OK(T data) {
        Result<T> result = new Result<>(ResultCode.OK);
        result.flag = true;
        result.data = data;
        return result;
    }

    public static <T> Result<T> SYS_ERROR() {
        Result<T> result = new Result<>(ResultCode.SYS_ERROR);
        result.flag = false;
        return result;
    }

    public static <T> Result<T> CUSTOM_ERROR() {
        Result<T> result = new Result<>(ResultCode.CUSTOM_ERROR);
        result.flag = false;
        return result;
    }

    public static <T> Result<T> CUSTOM_ERROR(String message) {
        Result<T> result = new Result<>(ResultCode.CUSTOM_ERROR);
        result.flag = false;
        result.message = message;
        return result;
    }

    public static <T> Result<T> CUSTOM(Boolean flag, String message, T t) {
        Result<T> result = new Result<>(ResultCode.OK);
        result.setFlag(flag);
        result.setMessage(message);
        result.setData(t);
        return result;
    }

//    public static <T> Result<T> CUSTOM_ERROR(Integer code, String message) {
//        Result<T> result = new Result<>();
//        result.flag = false;
//        result.code = code;
//        result.message = message;
//        return result;
//    }

}
